#ifndef OS_APPLICATION_H
#define OS_APPLICATION_H
/* define name application paths */
#include "main.h"
#include <QApplication>
#include <QtGui>
#include "gmain.h"

/* catch event from QCoreApplication if haveit more */
#define osApplication \
    (static_cast<OS_application*>(QCoreApplication::instance()))
    
class OS_application : public QApplication
{
    Q_OBJECT
//
public:
 OS_application( int &argc, char **argv )
  : QApplication(argc, argv)
{
        /* QSetting setup to access */
        setOrganizationName(_ORGANIZATION_NAME_);
        setOrganizationDomain(_PROGRAM_NAME_DOMAINE_);
        setApplicationName(_PROGRAM_NAME_);
        QString soko = QString(setter.value("WinStyle").toString());
        QStringList path;
        #if defined Q_WS_MAC
        path.append(QApplication::applicationDirPath());
        QDir dir(QApplication::applicationDirPath());
        dir.cdUp();
        path.append(dir.absolutePath());
        dir.cd("plugins");
        path.append(dir.absolutePath());
        LogEvents(QString("Plug in path %1").arg(dir.absolutePath()));
        dir.cdUp();
        path.append(dir.absolutePath());
        setLibraryPaths(path);
        QDir::setCurrent(dir.absolutePath());   /* here down -> Frameworks */
        LogEvents(QString("set current path %1").arg(dir.absolutePath()));
        #endif 
    
    
    
    
    
        /* start QApplication */
        Gmain::self()->setWindowTitle(_PROGRAM_TITLE_);   /* static QPointer<Gmain> _self; */
        if (soko.size() > 0 ) { 
        setStyle(QStyleFactory::create(soko));   
        }            
        Gmain::self()->show();  /* an show its */
}
protected:
bool event(QEvent *ev)
{
    ///////LogEvents( QString("normal event first  ev->type() = %1").arg(ev->type()));
    /* http://doc.trolltech.com/4.2/qevent.html#Type-enum 170 event resize context ecc...*/
    bool eaten;
    switch (ev->type()) {
    case QEvent::FileOpen:
        /* nr 116 */
        //////filecheck.fromLocalFile(QString());
        osfile = static_cast<QFileOpenEvent *>(ev)->file();
        Gmain::self()->OpenFile(osfile);  /* open file */
        Gmain::self()->RemoveNullFile();  /* clean null file and empyty widget at set correct if nullfile  */
        //////////LogEvents( QString("QEvent::FileOpen  nr. %1 catch file \"%2\" ").arg(QEvent::FileOpen).arg(osfile));
        eaten = true;
        break;
    case QEvent::Close: {
        QCloseEvent *closeEvent = static_cast<QCloseEvent *>(ev);
        /* clean all choise and close dbs if is open */
        Gmain::self()->cleanall();
        eaten = true;
        break;
    }
    default:
        eaten = QApplication::event(ev);
        break;
    }
    return eaten;
}
private:
    QUrl filecheck;
    QString osfile;
    QSettings setter;
/*  leave this outside debug  by open application write 135 line of events */
/**/
bool LogEvents( const QString xml )
{
    const QString fullFileName = QDir::homePath() + "/_todaylog/event.log";
    if (fullFileName.contains("/", Qt::CaseInsensitive)) {
    QString ultimacartellaaperta = fullFileName.left(fullFileName.lastIndexOf("/"))+"/";
    QDir dira(ultimacartellaaperta);
    if ( dira.mkpath(ultimacartellaaperta) ) { } else {
    return false;
    }
    }
    QString data = xml+"\n";
    QFile f( fullFileName );
	if ( f.open( QFile::Append | QFile::Text ) )
	{
		QTextStream sw( &f );
		sw << data;
		f.close();
		return true;
	}
	return false;
    
}
/**/


};
//
#endif // OS_APPLICATION_H

